package com.skycong.hello77.datepicker;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;

import com.skycong.hello77.R;
import com.skycong.hello77.common.StringUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 自定义年月选择弹框
 * Created by Kiki on 2018/6/7.
 */

public class DatePickerDialog extends Dialog {

    public OwnDataView dateYear;//年
    public OwnDataView dateMonth;//月
    public Button btnOk;//确定按钮
    public Button btnCancel;//取消按钮
    public Context context;
    public List<String> years = new ArrayList<>();
    public List<String> months = new ArrayList<>();
    public String year;//选择的年份
    public String month;//选择的月份

    public DatePickerDialog(Context context, final SelectTimeDialogListener listener) {
        super(context, R.style.MyDialog);
        setContentView(R.layout.layout_datepicker);
        setCanceledOnTouchOutside(true);
        setDialogWidth(context);
        dateYear = (OwnDataView) findViewById(R.id.dateYear);
        dateMonth = (OwnDataView) findViewById(R.id.dateMonth);
        btnOk = (Button) findViewById(R.id.btn_confirm_commoncanceldialog);
        btnCancel = (Button) findViewById(R.id.btn_cancel_commoncanceldialog);
        setYearOrData();
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.selectTimeConfirm(year + "/" + month);
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCancel();
                }
            }
        });

    }

    //设置年月数据
    public void setYearOrData() {
        year = StringUtil.getCurrentYear();
        month = StringUtil.getCurrentMonth();
        setYear();
        setMonth();
        //当前年月
        if (years != null && years.size() > 0) {
            dateYear.setData(years, years.indexOf(year));
            Log.i("msg","查找当前年的位置"+years.indexOf(year));
        }
        dateYear.setOnSelectListener(new OwnDataView.onSelectListener() {
            @Override
            public void onSelect(String date) {
                year = date;
            }
        });
        if (months != null && months.size() > 0) {
            dateMonth.setData(months, months.indexOf(month));
        }
            dateMonth.setOnSelectListener(new OwnDataView.onSelectListener() {
                @Override
                public void onSelect(String date) {
                    month = date;
                }
            });


    }

    private void setMonth() {
        if (month.equals("04") || month.equals("05")
                || month.equals("06") || month.equals("07")
                || month.equals("08") || month.equals("09")) {
            months.add("01");
            months.add("02");
            months.add("03");
            months.add("04");
            months.add("05");
            months.add("06");
            months.add("07");
            months.add("08");
            months.add("09");
            months.add("10");
            months.add("11");
            months.add("12");
        } else {
            months.add("07");
            months.add("08");
            months.add("09");
            months.add("10");
            months.add("11");
            months.add("12");
            months.add("01");
            months.add("02");
            months.add("03");
            months.add("04");
            months.add("05");
            months.add("06");
        }
    }

    private void setYear() {
        //Integer.valueOf(StringUtil.getCurrentYear())，年份不超过当前年份
        for (int i = 1970; i <= Integer.valueOf(StringUtil.getCurrentYear()); i++) {
            years.add(i + "");
        }
    }

    private void setDialogWidth(Context context) {
        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        win.getDecorView().setPadding(0, 0, 0, 0);

        /** 获取屏幕的宽和高 */
        DisplayMetrics dm = new DisplayMetrics();
        Activity activity = (Activity) context;
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        lp.width = dm.widthPixels * 3 / 4;
        win.setAttributes(lp);
    }

    public interface SelectTimeDialogListener {
        void onCancel();

        void selectTimeConfirm(String timeStr);
    }

}
